package com.thd.springboottest.camunda.utils;

import org.apache.commons.lang3.StringUtils;

/**
 * com.thd.springboottest.camunda.utils.PropertyUtils
 *
 * @author: wanglei62
 * @DATE: 2022/5/30 9:27
 **/
public class PropertyUtils {
    private static final char UNDERLINE = '_';

    public PropertyUtils() {
    }

    public static String camel2Underline(String text) {
        if (StringUtils.isNotBlank(text)) {
            int len = text.length();
            StringBuffer sb = new StringBuffer(len + len >> 1);

            for(int i = 0; i < len; ++i) {
                char c = text.charAt(i);
                if (Character.isUpperCase(c)) {
                    sb.append('_').append(Character.toLowerCase(c));
                } else {
                    sb.append(c);
                }
            }

            return sb.toString();
        } else {
            return text;
        }
    }

    public static String underline2Camel(String text) {
        if (!StringUtils.isNotBlank(text)) {
            return text;
        } else {
            int len = text.length();
            StringBuilder sb = new StringBuilder(len);

            for(int i = 0; i < len; ++i) {
                char c = text.charAt(i);
                if (c == '_') {
                    ++i;
                    if (i < len) {
                        sb.append(Character.toUpperCase(text.charAt(i)));
                        continue;
                    }
                }

                sb.append(c);
            }

            return sb.toString();
        }
    }
}
